package com.snail.ddclock

import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.IBinder
import com.snail.commons.entity.MyTimer
import java.text.SimpleDateFormat
import java.util.*


/**
 * 描述:
 * 时间: 2018/10/17 21:36
 * 作者: zengfansheng
 */
class TimerService : Service() {
    private val localBinder = LocalBinder()
    private var running = false
    private var notificationBuilder: Notification.Builder? = null
    private var nm: NotificationManager? = null
    private val timer = MyTimer()
    
    inner class LocalBinder : Binder() {
        fun getService(): TimerService {
            return this@TimerService
        }
    } 
    
    override fun onBind(intent: Intent?): IBinder? {
        return localBinder
    }
    
    override fun onCreate() {
        initNotify()
        running = true
        var count = 0
        timer.startTimer(0, 1000, object : MyTimer.UiTimerTaskCallback {
            override fun runOnUiTimerTask() {
                val random = Random().nextInt(30) + 40
                if (count++ >= random) {
                    count = 0
                    val calendar = Calendar.getInstance()
                    val hour = calendar.get(Calendar.HOUR_OF_DAY)
                    val minute = calendar.get(Calendar.MINUTE)
                    val week = calendar.get(Calendar.DAY_OF_WEEK)
                    if (week != Calendar.SUNDAY && week != Calendar.SATURDAY) {
                        if ((hour == 8 && minute == 4) || (hour == 18 && minute == 31)) {
                            updateNotify()
                        }
                    }                    
                }
            }
        })
    }
    

    override fun onDestroy() {
        running = false
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            nm?.deleteNotificationChannel("1")
        }
        nm?.cancel(1)
    }

    private fun initNotify() {
        nm = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("1", "Channel1", NotificationManager.IMPORTANCE_DEFAULT)
            nm?.createNotificationChannel(channel)
            channel.setShowBadge(false)
        }
        val msgIntent = Intent(this, MainActivity::class.java)
        msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        val contentIntent = PendingIntent.getActivity(this, 0, msgIntent, 0)
        notificationBuilder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.Builder(this, "1")
                    .setContentIntent(contentIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setOngoing(true)
        } else {
            Notification.Builder(this)
                    .setContentIntent(contentIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setOngoing(true)
        }
    }

    private fun updateNotify() {
        //判断是否通话中
        if (running) {
            val content = SimpleDateFormat("HH:mm", Locale.CHINA).format(System.currentTimeMillis())
            notificationBuilder?.setContentTitle("后台运行中...")?.setContentText(content)
            nm?.notify(1, notificationBuilder!!.build())
        }
    }
}